<?php /*0*/

/**
  *	This file contains a class
  *
  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
  *	@version 0.1
  */

if ( !defined( 'SECURITY' ) ) {
	die( 'Direct access to this script is not allowed!' );
}

/**
  *	This class is responsible for simple file operations
  *
  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
  *	@version 0.1
  */
class SimpleFile {

	/**
	  *	This method clears a file
	  * <br />
	  * If file exists, it becomes empty, but if there is no such file, it is created
	  *
	  * <br /><br />
	  *	Note that parameters are located in an associative array
	  *
	  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
	  *	@access public
	  *	@version 0.0
	  *
	  * @static
	  *
	  * @param array $arguments - this is an associative array <br /><br />
	  * It contains: <br />
	  * 	- address - string file address <br />
	  *
	  *	@return bool true on success
	  */
	public static function clear( $arguments ) {
		
		if ( !$address = Arguments::getArgument( array( 'title' => 'address', 'argument_array' => $arguments ) ) ) {
			return false;
		}
		
		if ( !is_string( $address ) ) {
			return false;
		}
		
		$address = SimpleFile::getFullFileAddress( array( 'address' => $address ) );
		
		if ( !$handler =@ fopen( $address, 'w' ) ) {
			return false;
		}
		
		fclose( $handler );
		
		return true;
		
	}
	
	/**
	  *	This method creates a file
	  * <br />
	  * If file exists, it becomes empty, but if there is no such file, it is created
	  *
	  * <br /><br />
	  *	Note that parameters are located in an associative array
	  *
	  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
	  *	@access public
	  *	@version 0.0
	  *
	  * @static
	  *
	  * @param array $arguments - this is an associative array <br /><br />
	  * It contains: <br />
	  * 	- address - string file address <br />
	  *
	  *	@return bool true on success
	  */
	public static function create( $arguments ) {
		
		return SimpleFile::clear( $arguments );
		
	}
	
	/**
	  *	This method appends file with new information
	  *
	  * <br /><br />
	  *	Note that parameters are located in an associative array
	  *
	  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
	  *	@access public
	  *	@version 0.0
	  *
	  * @static
	  *
	  * @param array $arguments - this is an associative array <br /><br />
	  * It contains: <br />
	  * 	- address - string file address <br />
	  * 	- lines - array of string lines
	  *
	  *	@return bool true on success
	  */
	public static function appendFile( $arguments ) {
		
		if ( !$address = Arguments::getArgument( array( 'title' => 'address', 'argument_array' => $arguments ) ) ) {
			return false;
		}
		
		if ( !is_string( $address ) ) {
			return false;
		}
		
		$address = SimpleFile::getFullFileAddress( array( 'address' => $address ) );
		
		$lines = Arguments::getArgument( array( 'title' => 'lines', 'argument_array' => $arguments ) );
		
		if ( !$lines || !is_array( $lines ) ) {
			return false;
		}
		
		clearstatcache();
		if ( false === $file_size =@ filesize( $address ) ) {
			return false;
		}
		
		foreach( $lines as $line ) {
			if ( !is_string( $line ) && !is_numeric( $line ) ) {
				return false;
			}
		}
		
		if ( !$handler =@ fopen( $address, 'a' ) ) {
			return false;
		}
		
		foreach( $lines as $line ) {
			
			if ( $file_size != 0 ) {
				fwrite( $handler, "\n" );
			} else {
				$file_size = 1;
			}
			
			fwrite( $handler, $line );
			
		}
		
		fclose( $handler );
		
		return true;
		
	}
	
	/**
	  *	This method reads the whole file and puts its lines into array
	  *	<br />
	  *	This method ignores empty lines by default
	  *
	  * <br /><br />
	  *	Note that parameters are located in an associative array
	  *
	  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
	  *	@access public
	  *	@version 0.1
	  *
	  * @static
	  *
	  * @param array $arguments - this is an associative array <br /><br />
	  * It contains: <br />
	  * 	- address - string file address <br />
	  * 	- include_empty - bool flag which can enable including empty lines if true <br />
	  * 	- external_address - bool flag which tells function that file is outside the project <br />
	  *
	  *	@return array/false file lines or false on error <br /><br />
	  * It contains: <br />
	  *		- [(key)line number] => [(string)line]<br /><br />
	  */
	public static function getLines( $arguments ) {
		
		if ( !$address = Arguments::getArgument( array( 'title' => 'address', 'argument_array' => $arguments ) ) ) {
			return false;
		}
		
		if ( !is_string( $address ) ) {
			return false;
		}
		
		$external_address = Arguments::getArgument( array( 'title' => 'external_address', 'argument_array' => $arguments ) );
		
		if ( !$external_address ) {
			$address = SimpleFile::getFullFileAddress( array( 'address' => $address ) );
		}
		
		$include_empty = Arguments::getArgument( array( 'title' => 'include_empty', 'argument_array' => $arguments ) );
		
		if ( !$handler =@ fopen( $address, 'r' ) ) {
			return false;
		}
		
		$result = array();
		$line_id = 1;
		
		while ( !feof( $handler ) ) {
			$line = trim( fgets( $handler ) );
			if ( ( strlen( $line ) > 0 ) || ( $include_empty ) ) {
				$result[$line_id] = $line;
				$line_id++;
			}
		}
		
		fclose( $handler );
		
		return $result;
	
	}
	
	/**
	  *	This method gets the list of files and directories
	  *
	  * <br /><br />
	  *	Note that parameters are located in an associative array
	  *
	  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
	  *	@access public
	  *	@version 0.0
	  *
	  * @static
	  *
	  * @param array $arguments - this is an associative array <br /><br />
	  * It contains: <br />
	  * 	- address - string directory address <br />
	  * 	- external_address - bool flag which tells function that file is outside the project <br />
	  *
	  *	@return array/false file list or false on error <br /><br />
	  * It contains: <br />
	  *		- [(key)file/directory number] => [(string)file/directory name]<br /><br />
	  */
	public static function getFiles( $arguments ) {
	
		if ( !$address = Arguments::getArgument( array( 'title' => 'address', 'argument_array' => $arguments ) ) ) {
			return false;
		}
		
		if ( !is_string( $address ) ) {
			return false;
		}
		
		$external_address = Arguments::getArgument( array( 'title' => 'external_address', 'argument_array' => $arguments ) );
		
		if ( !$external_address ) {
			$address = SimpleFile::getFullFileAddress( array( 'address' => $address ) );
		}
		
		$result = array();
		$item_id = 1;
		
		if ( !$directory =@ opendir( $address ) ) {
			return false;
		}
		
		$ignored_files = array (
			'.',
			'..',
			'.svn',
			'.htaccess',
		);
		
		while ( false !== ( $item = readdir( $directory ) ) ) {
			if ( !in_array( $item, $ignored_files ) ) {
				$result[$item_id] = $item;
				$item_id++;
			}
		}
		
		closedir( $directory );
		
		return $result;
		
	}
	
	/**
	  *	This method deletes a file
	  *
	  * <br /><br />
	  *	Note that parameters are located in an associative array
	  *
	  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
	  *	@access public
	  *	@version 0.0
	  *
	  * @static
	  *
	  * @param array $arguments - this is an associative array <br /><br />
	  * It contains: <br />
	  * 	- address - string file address <br />
	  *
	  *	@return bool true on success
	  */
	public static function deleteFile( $arguments ) {
		
		if ( !$address = Arguments::getArgument( array( 'title' => 'address', 'argument_array' => $arguments ) ) ) {
			return false;
		}
		
		if ( !is_string( $address ) ) {
			return false;
		}
		
		$address = SimpleFile::getFullFileAddress( array( 'address' => $address ) );
		
		return @unlink( $address );
		
	}
	
	/**
	  *	This method prepares file or directory address for usage
	  *
	  * <br /><br />
	  *	Note that parameters are located in an associative array
	  *
	  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
	  *	@access public
	  *	@version 0.0
	  *
	  * @static
	  *
	  * @param array $arguments - this is an associative array <br /><br />
	  * It contains: <br />
	  * 	- address - string file or directory address <br />
	  *
	  *	@return string/false string address or false on error
	  */
	public static function getFullFileAddress( $arguments ) {
		
		$address = Arguments::getArgument( array( 'title' => 'address', 'argument_array' => $arguments ) );
		
		if ( !$address || !is_string( $address ) ) {
			return false;
		}
		
		$file_root_len = strlen( constant( 'ROOT_DIR' ) . '/' );
		
		if ( !( ( ( strlen( $address ) >= $file_root_len ) && ( substr( $address, 0, $file_root_len ) == constant( 'ROOT_DIR' ) . '/' ) ) || ( $address == constant( 'ROOT_DIR' ) ) ) ) {
			$address = Filter::normaliseAddress( array( 'address' => $address, 'start_slash' => true ) );
			$address = constant( 'ROOT_DIR' ) . '/' . $address;
		} else {
			$address = Filter::normaliseAddress( array( 'address' => $address, 'start_slash' => false ) );
		}
		
		$path_array = explode( '/', $address );
		
		if ( !empty( $path_array ) ) {
			for ( $i = 0; $i <= count( $path_array ) - 1; $i++ ) {
				if ( $path_array[$i] == '.' ) {
					unset( $path_array[$i] );
				} elseif ( $path_array[$i] == '..' ) {
					unset( $path_array[$i] );
					if ( isset( $path_array[$i - 1] ) ) {
						unset( $path_array[$i - 1] );
					}
				}
			}
			$address = implode( '/', $path_array );
		}
		
		return $address;
		
	}

}

?>